﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.GERP.GIMS.MODEL;
using KPIS.DBM;
using System.Configuration;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_IcmInventoryRepair
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_IcmInventoryRepair> GetListFillByStatusRepairCode(string repair_status)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_IcmInventoryRepair> infoList = new List<M_IcmInventoryRepair>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_repair_seq,asset_code,repair.icm_dealer_repair_seq,repair.job_site_seq,repair.icm_wh_seq "
                        +" ,icm_repair_date,icm_repair_descr,icm_cause,icm_repair_remark,repair_amount "
                        +" ,repair_discount,repair.repair_status,case when (js.job_site_name is not null) then js.job_site_name else wh.icm_wh_name end OrganizeSendRepair "
                        + " ,ct.icm_con_name_th,rs.status_name,repair.repair_status "
                        +" FROM icm_inventory_repair repair "
                        +" LEFT JOIN hrm_mp_job_sites js ON repair.job_site_seq = js.job_site_seq "
                        +" LEFT JOIN icm_mas_warehouse wh ON repair.icm_wh_seq = wh.icm_wh_seq "
                        +" LEFT JOIN icm_mas_contact ct ON repair.icm_dealer_repair_seq = ct.icm_con_seq  "
                        +" LEFT JOIN icm_sys_repair_status rs ON repair.repair_status = rs.repair_status "
                        + " WHERE repair.record_status <> 'D' and repair.repair_status = '" + repair_status + "'";

                //LIKE '%' '" + @requisition_code + "'  '%' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmInventoryRepair info = new M_IcmInventoryRepair();
                        info.icm_repair_seq = Convert.ToInt32(drc[i]["icm_repair_seq"]);
                        info.icm_repair_date = Convert.ToDateTime(drc[i]["icm_repair_date"]);
                        info.organize_send_repair = drc[i]["OrganizeSendRepair"].ToString();
                        info.icm_dealer_repair_name = drc[i]["icm_con_name_th"].ToString();
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.icm_repair_descr = drc[i]["icm_repair_descr"].ToString();
                        info.icm_repair_remark = drc[i]["icm_repair_remark"].ToString();
                        info.icm_cause = drc[i]["icm_repair_remark"].ToString();
                        info.repair_status_name = drc[i]["status_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_IcmInventoryRepair GetListFillBySeq(Nullable<Int32> icm_repair_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_IcmInventoryRepair info = new M_IcmInventoryRepair();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_repair_seq", icm_repair_seq);

                strSQL =  " SELECT icm_repair_seq,icm_repair_date,icm_receive_date,asset_code,icm_repair_descr,icm_cause,repair_amount "
                         +" ,repair_discount,repair.job_site_seq,repair.icm_wh_seq,repair.icm_dealer_repair_seq "
                         +" ,icm_repair_remark,js.job_site_name,wh.icm_wh_name,ct.icm_con_name_th,repair.repair_status,rs.status_name "
                         +" FROM icm_inventory_repair repair "
                         +" LEFT JOIN hrm_mp_job_sites js ON repair.job_site_seq = js.job_site_seq "
                         +" LEFT JOIN icm_mas_warehouse wh ON repair.icm_wh_seq = wh.icm_wh_seq "
                         +" LEFT JOIN icm_mas_contact ct ON repair.icm_dealer_repair_seq = ct.icm_con_seq "
                         +" LEFT JOIN icm_sys_repair_status rs ON repair.repair_status = rs.repair_status "
                         + " WHERE repair.record_status <> 'D' and repair.icm_repair_seq = @icm_repair_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_repair_seq = Convert.ToInt32(drc[0]["icm_repair_seq"]);
                    info.icm_repair_date = Convert.ToDateTime(drc[0]["icm_repair_date"]);
                    info.icm_receive_date = cn.ConvertNullDateTime(drc[0]["icm_receive_date"]);
                    info.asset_code = drc[0]["asset_code"].ToString();
                    info.icm_repair_descr = drc[0]["icm_repair_descr"].ToString();
                    info.icm_cause = drc[0]["icm_cause"].ToString();
                    info.repair_amount = cn.ConvertNullDouble(drc[0]["repair_amount"]);
                    info.repair_discount = cn.ConvertNullDouble(drc[0]["repair_discount"]);
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                    info.icm_wh_seq = cn.ConvertNullInt(drc[0]["icm_wh_seq"]);
                    info.icm_dealer_repair_seq = cn.ConvertNullInt(drc[0]["icm_dealer_repair_seq"]);
                    info.icm_repair_remark = drc[0]["icm_repair_remark"].ToString();
                    info.repair_status_name = drc[0]["repair_status"].ToString();
                    info.icm_dealer_repair_name = drc[0]["icm_con_name_th"].ToString();
                    info.job_site_name = drc[0]["job_site_name"].ToString();
                    info.warehouse_name = drc[0]["icm_wh_name"].ToString();
                    info.repair_status = drc[0]["repair_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public void Insert_Inventory_Repair(M_IcmInventoryRepair info)
        {
            string strSQL="";
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(15);
                dbManager.AddParameters(0, "@icm_repair_date", info.icm_repair_date);
                dbManager.AddParameters(1, "@icm_receive_date", info.icm_receive_date);
                dbManager.AddParameters(2, "@asset_code", info.asset_code);
                dbManager.AddParameters(3, "@icm_repair_descr", info.icm_repair_descr);
                dbManager.AddParameters(4, "@icm_cause", info.icm_cause);
                dbManager.AddParameters(5, "@repair_amount", info.repair_amount);
                dbManager.AddParameters(6, "@repair_discount", info.repair_discount);
                dbManager.AddParameters(7, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(8, "@icm_wh_seq", info.icm_wh_seq);
                dbManager.AddParameters(9, "@icm_dealer_repair_seq", info.icm_dealer_repair_seq);
                dbManager.AddParameters(10, "@icm_repair_remark", info.icm_repair_remark);
                dbManager.AddParameters(11, "@repair_status", info.repair_status);
                dbManager.AddParameters(12, "@record_status", info.record_status);
                dbManager.AddParameters(13, "@created_by", info.created_by);
                dbManager.AddParameters(14, "@created_when", info.created_when);
                
                strSQL =  " INSERT INTO icm_inventory_repair "
                        + " (icm_repair_date,icm_receive_date,asset_code,icm_repair_descr,icm_cause "
                        + " ,repair_amount,repair_discount,job_site_seq,icm_wh_seq "
                        + " ,icm_dealer_repair_seq,icm_repair_remark,repair_status,record_status,created_by,created_when) "
                        + " VALUES "
                        + " (@icm_repair_date,@icm_receive_date,@asset_code,@icm_repair_descr,@icm_cause "
                        + " ,@repair_amount,@repair_discount,@job_site_seq,@icm_wh_seq "
                        + " ,@icm_dealer_repair_seq,@icm_repair_remark,@repair_status,@record_status,@created_by,@created_when) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update_Inventory_Repair(M_IcmInventoryRepair info)
        {
            string strSQL="";
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(16);
                dbManager.AddParameters(0, "@icm_repair_seq", info.icm_repair_seq);
                dbManager.AddParameters(1, "@icm_repair_date", info.icm_repair_date);
                dbManager.AddParameters(2, "@icm_receive_date", info.icm_receive_date);
                dbManager.AddParameters(3, "@asset_code", info.asset_code);
                dbManager.AddParameters(4, "@icm_repair_descr", info.icm_repair_descr);
                dbManager.AddParameters(5, "@icm_cause", info.icm_cause);
                dbManager.AddParameters(6, "@repair_amount", info.repair_amount);
                dbManager.AddParameters(7, "@repair_discount", info.repair_discount);
                dbManager.AddParameters(8, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(9, "@icm_wh_seq", info.icm_wh_seq);
                dbManager.AddParameters(10, "@icm_dealer_repair_seq", info.icm_dealer_repair_seq);
                dbManager.AddParameters(11, "@icm_repair_remark", info.icm_repair_remark);
                dbManager.AddParameters(12, "@repair_status", info.repair_status);
                dbManager.AddParameters(13, "@record_status", info.record_status);
                dbManager.AddParameters(14, "@updated_by", info.updated_by);
                dbManager.AddParameters(15, "@updated_when", info.updated_when);

                 strSQL=  " UPDATE icm_inventory_repair "
                        + " SET icm_repair_date = @icm_repair_date "
                        + " ,icm_receive_date = @icm_receive_date "
                        + " ,asset_code = @asset_code "
                        + " ,icm_repair_descr =@icm_repair_descr "
                        + " ,icm_cause = @icm_cause "
                        + " ,repair_amount = @repair_amount "
                        + " ,repair_discount = @repair_discount "
                        + " ,job_site_seq = @job_site_seq "
                        + " ,icm_wh_seq = @icm_wh_seq "
                        + " ,icm_dealer_repair_seq = @icm_dealer_repair_seq "
                        + " ,icm_repair_remark = @icm_repair_remark "
                        + " ,repair_status = @repair_status "
                        + " ,record_status = @record_status "
                        + " ,updated_by = @updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_repair_seq = @icm_repair_seq ";

                 dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete_Inventory_Repair(M_IcmInventoryRepair info)
        {
            string strSQL = "";
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@icm_repair_seq", info.icm_repair_seq);
                dbManager.AddParameters(1, "@record_status", info.record_status);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_inventory_repair "
                       + " SET record_status = @record_status "
                       + " ,updated_by = @updated_by "
                       + " ,updated_when = @updated_when "
                       + " WHERE icm_repair_seq = @icm_repair_seq ";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
